package org.luckyjourney.service.user.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.checkerframework.checker.units.qual.A;
import org.luckyjourney.entity.user.Favorites;
import org.luckyjourney.entity.user.FavoritesVideo;
import org.luckyjourney.entity.video.Video;
import org.luckyjourney.entity.video.VideoStar;
import org.luckyjourney.entity.video.vo.VideoStartCountVO;
import org.luckyjourney.entity.vo.UserVO;
import org.luckyjourney.exception.BaseException;
import org.luckyjourney.holder.UserHolder;
import org.luckyjourney.mapper.user.FavoritesMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.luckyjourney.mapper.user.FavoritesVideoMapper;
import org.luckyjourney.mapper.video.VideoMapper;
import org.luckyjourney.service.user.FavoritesService;
import org.luckyjourney.service.user.FavoritesVideoService;
import org.luckyjourney.service.user.UserService;
import org.luckyjourney.service.video.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xhy
 * @since 2023-10-30
 */
@Service
public class FavoritesServiceImpl extends ServiceImpl<FavoritesMapper, Favorites> implements FavoritesService {


    @Autowired
    private FavoritesVideoService favoritesVideoService;
    @Autowired
    UserService userService;
    @Autowired
    FavoritesVideoMapper favoritesVideoMapper;

    @Autowired
    private FavoritesMapper favoritesMapper;


    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private VideoService videoService;

    @Override
    @Transactional
    public void remove(Long id, Long userId) {

        // 不能删除默认收藏夹
        final Favorites favorites = getOne(new LambdaQueryWrapper<Favorites>().eq(Favorites::getId, id).eq(Favorites::getUserId, userId));
        if (favorites.getName().equals("默认收藏夹")){
            throw new BaseException("默认收藏夹不允许被删除");
        }

        final boolean result = remove(new LambdaQueryWrapper<Favorites>().eq(Favorites::getId, id).eq(Favorites::getUserId, userId));
        // 如果能删除成功说明是自己的收藏夹
        if (result){
            favoritesVideoService.remove(new LambdaQueryWrapper<FavoritesVideo>().eq(FavoritesVideo::getFavoritesId,id));
        }else {
            throw new BaseException("你小子想删别人的收藏夹?");
        }
    }

    @Override
    public List<Favorites> listByUserId(Long userId) {
        // 查出收藏夹id
        final List<Favorites> favorites = list(new LambdaQueryWrapper<Favorites>().eq(Favorites::getUserId, userId));
        if (ObjectUtils.isEmpty(favorites)) return Collections.EMPTY_LIST;
        // 根据收藏夹id获取对应数
        final List<Long> fIds = favorites.stream().map(Favorites::getId).collect(Collectors.toList());
        final Map<Long, Long> fMap = favoritesVideoService.list(new LambdaQueryWrapper<FavoritesVideo>().in(FavoritesVideo::getFavoritesId, fIds))
                .stream().collect(Collectors.groupingBy(FavoritesVideo::getFavoritesId,
                        Collectors.counting()));
        // 计算对应视频总数
        for (Favorites favorite : favorites) {
            final Long videoCount = fMap.get(favorite.getId());
            favorite.setVideoCount(videoCount == null ? 0 :videoCount);
        }

        return favorites;
    }

    @Override
    public List<Long> listVideoIds(Long favoritesId, Long userId) {

        // 不直接返回中间表是为了隐私性 (当前没实现收藏夹公开功能)

        // 校验
        final Favorites favorites = getOne(new LambdaQueryWrapper<Favorites>().eq(Favorites::getId, favoritesId).eq(Favorites::getUserId, userId));
        if (favorites == null){
            throw new BaseException("收藏夹为空");
        }

        final List<Long> videoIds = favoritesVideoService.list(new LambdaQueryWrapper<FavoritesVideo>().eq(FavoritesVideo::getFavoritesId, favoritesId))
                .stream().map(FavoritesVideo::getVideoId).collect(Collectors.toList());

        return videoIds;
    }

    @Override
    public VideoStartCountVO favorites(Long fId, Long vId) {

        final Long userId = UserHolder.get();
        UserVO userVO = userService.getInfo(userId);
        Integer ModuleId = userVO.getModuleId();
        LambdaQueryWrapper<FavoritesVideo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FavoritesVideo::getVideoId, vId);
        queryWrapper.eq(FavoritesVideo::getUserId, userId);
        queryWrapper.eq(FavoritesVideo::getModuleId, ModuleId);
        //获取收藏数
        FavoritesVideo favoritesVideo1 = favoritesVideoMapper.selectOne(queryWrapper);
        FavoritesVideo video = new FavoritesVideo();
        video.setVideoId(vId);
        video.setFavoritesId(fId);
        video.setUserId(userId);
        video.setModuleId(ModuleId);
        if (ObjectUtil.isEmpty(favoritesVideo1)) {
            favoritesVideoMapper.insert(video);
            VideoStartCountVO countVO = getVideoStartCountVO(vId);
            countVO.setABoolean(true);
            return countVO;
        } else {
            favoritesVideoMapper.delete(queryWrapper);
            VideoStartCountVO countVO = getVideoStartCountVO(vId);
            countVO.setABoolean(false);
            return countVO;
        }
    }

    private VideoStartCountVO getVideoStartCountVO(Long vId) {
        //查询是那条视频
        Video video = videoMapper.selectById(vId);
        LambdaQueryWrapper<FavoritesVideo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FavoritesVideo::getVideoId, vId);
        Integer count = favoritesVideoMapper.selectCount(wrapper);
        Long counts= Long.valueOf(count);
        //将值赋给视频
        video.setFavoritesCount(counts);
        videoService.saveOrUpdate(video);
        VideoStartCountVO countVO = new VideoStartCountVO();
        countVO.setFavoritesCount(count);
        return countVO;
    }

    @Override
    public Boolean favoritesState(Long videoId, Long userId) {
        if (userId == null) return false;
        return favoritesVideoService.count(new LambdaQueryWrapper<FavoritesVideo>().eq(FavoritesVideo::getVideoId,videoId).eq(FavoritesVideo::getUserId,userId)) == 1;
    }

    @Override
    public void exist(Long userId, Long fId) {
        final int count = count(new LambdaQueryWrapper<Favorites>().eq(Favorites::getUserId, userId).eq(Favorites::getId, fId));
        if (count == 0){
            throw new BaseException("收藏夹选择错误");
        }
    }
}
